﻿Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
<Serializable()> Public Class Group
    Implements ICloneable

    Private m_Name As String = ""
    Private m_GroupID As String
    Private m_GroupSource(-1) As GroupSource

    Private m_IsTypeDay As Boolean = True
    Private m_IsTypeHours As Boolean = True

    Private m_IsDayOver As Boolean = False
    Private m_IsHoursOver As Boolean = False
    ''' <summary>
    ''' 表示该组源是否没有非0的污染源
    ''' </summary>
    ''' <remarks></remarks>
    Private m_IsZero As Boolean = True
    Property IsTypeDay() As Boolean
        Get
            Return Me.m_IsTypeDay
        End Get
        Set(ByVal value As Boolean)
            Me.m_IsTypeDay = value
        End Set
    End Property
    Property IsTypeHours() As Boolean
        Get
            Return Me.m_IsTypeHours
        End Get
        Set(ByVal value As Boolean)
            Me.m_IsTypeHours = value
        End Set
    End Property

    ''' <summary>
    ''' 污染源组的名称
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            Me.m_Name = value
        End Set
    End Property
    ''' <summary>
    ''' 污染源组的ID
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property GroupID() As String
        Get
            Return Me.m_GroupID
        End Get
        Set(ByVal value As String)
            Me.m_GroupID = value
        End Set
    End Property
    ''' <summary>
    ''' 污染源数组中包含的所有污染源
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property GroupSource() As GroupSource()
        Get
            Return Me.m_GroupSource
        End Get
        Set(ByVal value As GroupSource())
            Me.m_GroupSource = value
        End Set
    End Property

    ''' <summary>
    ''' 表示该组源是否没有非0的污染源
    ''' </summary>
    ''' <remarks></remarks>
    Property IsZero() As Boolean
        Get
            Return Me.m_IsZero
        End Get
        Set(ByVal value As Boolean)
            Me.m_IsZero = value
        End Set
    End Property
    ''' <summary>
    ''' 计算日平均超标率
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property IsDayOver() As Boolean
        Get
            Return Me.m_IsDayOver
        End Get
        Set(ByVal value As Boolean)
            Me.m_IsDayOver = value
        End Set
    End Property
    ''' <summary>
    ''' 计算小时平均超标率
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property IsHoursOver() As Boolean
        Get
            Return Me.m_IsHoursOver
        End Get
        Set(ByVal value As Boolean)
            Me.m_IsHoursOver = value
        End Set
    End Property


    ''' <summary>
    ''' 在新建污染源分组对象时新建一个对应的污染物数组
    ''' </summary>
    ''' <param name="AllSource"></param>
    ''' <remarks></remarks>
    Public Sub IntialSource(ByVal AllSource As Source)
        '建立一个新的数组
        ReDim Me.m_GroupSource(AllSource.GetAllSourceCount - 1)
        For i As Integer = 0 To Me.m_GroupSource.Length - 1
            Me.m_GroupSource(i) = New GroupSource

        Next
        ResetSource(AllSource) '重新设置数据
    End Sub

    ''' <summary>
    ''' 根据用户的修改重新设置污染源分组中的数组
    ''' </summary>
    ''' <param name="AllSource"></param>
    ''' <remarks></remarks>
    Public Sub ResetSource(ByVal AllSource As Source)
        '建立一个新的数组
        Dim NewGroupSources(AllSource.GetAllSourceCount - 1) As GroupSource
        '建立一个已有的数组
        Dim OldGroupSources(-1) As GroupSource
        OldGroupSources = Me.m_GroupSource

        Dim n As Integer = 0
        For i As Integer = 0 To AllSource.PointSources.PointSource.Length - 1 '点源数组
            NewGroupSources(n) = New GroupSource
            NewGroupSources(n).SourceName = AllSource.PointSources.PointSource(i).SourceName
            NewGroupSources(n).SourceID = AllSource.PointSources.PointSource(i).Location.Srcid
            If IsAlreadySelected(NewGroupSources(n), OldGroupSources) = True Then
                NewGroupSources(n).Selected = True
            End If
            n = n + 1
        Next
        For i As Integer = 0 To AllSource.VolumeSources.VolumeSource.Length - 1 '体源数组
            NewGroupSources(n) = New GroupSource
            NewGroupSources(n).SourceName = AllSource.VolumeSources.VolumeSource(i).SourceName
            NewGroupSources(n).SourceID = AllSource.VolumeSources.VolumeSource(i).Location.Srcid
            If IsAlreadySelected(NewGroupSources(n), OldGroupSources) = True Then
                NewGroupSources(n).Selected = True
            End If
            n = n + 1
        Next
        For i As Integer = 0 To AllSource.AreaSources.AreaSource.Length - 1 '面源数组
            NewGroupSources(n) = New GroupSource
            NewGroupSources(n).SourceName = AllSource.AreaSources.AreaSource(i).SourceName
            NewGroupSources(n).SourceID = AllSource.AreaSources.AreaSource(i).Location.Srcid
            If IsAlreadySelected(NewGroupSources(n), OldGroupSources) = True Then
                NewGroupSources(n).Selected = True
            End If
            n = n + 1
        Next
        For i As Integer = 0 To AllSource.AreaPolySources.AreaPolySource.Length - 1 '多边形源数组
            NewGroupSources(n) = New GroupSource
            NewGroupSources(n).SourceName = AllSource.AreaPolySources.AreaPolySource(i).SourceName
            NewGroupSources(n).SourceID = AllSource.AreaPolySources.AreaPolySource(i).Location.Srcid
            If IsAlreadySelected(NewGroupSources(n), OldGroupSources) = True Then
                NewGroupSources(n).Selected = True
            End If
            n = n + 1
        Next
        For i As Integer = 0 To AllSource.AreacircSources.AreaCircSource.Length - 1 '圆形源数组
            NewGroupSources(n) = New GroupSource
            NewGroupSources(n).SourceName = AllSource.AreacircSources.AreaCircSource(i).SourceName
            NewGroupSources(n).SourceID = AllSource.AreacircSources.AreaCircSource(i).Location.Srcid
            If IsAlreadySelected(NewGroupSources(n), OldGroupSources) = True Then
                NewGroupSources(n).Selected = True
            End If
            n = n + 1
        Next
        For i As Integer = 0 To AllSource.OpenPitSources.OpenPitSource.Length - 1 '敞口源数组
            NewGroupSources(n) = New GroupSource
            NewGroupSources(n).SourceName = AllSource.OpenPitSources.OpenPitSource(i).SourceName
            NewGroupSources(n).SourceID = AllSource.OpenPitSources.OpenPitSource(i).Location.Srcid
            If IsAlreadySelected(NewGroupSources(n), OldGroupSources) = True Then
                NewGroupSources(n).Selected = True
            End If
            n = n + 1
        Next
        For i As Integer = 0 To AllSource.LineSources.ArrayLineSources.Length - 1 '线源
            NewGroupSources(n) = New GroupSource
            NewGroupSources(n).SourceName = AllSource.LineSources.ArrayLineSources(i).SourceName
            NewGroupSources(n).SourceID = AllSource.LineSources.ArrayLineSources(i).Location.Srcid
            If IsAlreadySelected(NewGroupSources(n), OldGroupSources) = True Then
                NewGroupSources(n).Selected = True
            End If
            NewGroupSources(n).SourceType = 1 '线源
            'NewGroupSources(n).Line = AllSource.LineSources.ArrayLineSources(i).Clone  '把线源给组
            n = n + 1
        Next
        Me.m_GroupSource = NewGroupSources
    End Sub
    ''' <summary>
    ''' 判断1个给定的污染物是否在另外一个污染物数组中已经被选定
    ''' </summary>
    ''' <param name="AGroup"></param>
    ''' <param name="oldGroupSources"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function IsAlreadySelected(ByVal AGroup As GroupSource, ByVal oldGroupSources As GroupSource())
        For i As Integer = 0 To oldGroupSources.Length - 1
            If AGroup.SourceID = oldGroupSources(i).SourceID AndAlso oldGroupSources(i).Selected Then
                Return True
            End If
        Next

        Return False
    End Function
    ''' <summary>
    ''' 输出给定污染源数组的字符串
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function OutPutGroup(ByVal Sources As Source, ByVal name As String) As String
        m_IsZero = True '初
        Dim strG As String = ""
        For i As Integer = 0 To Me.m_GroupSource.Length - 1
            '先找出对应的污染源来，通过污染源是否为0来进行判断是否作为源组
            For n As Integer = 0 To Sources.PointSources.PointSource.Length - 1
                If Sources.PointSources.PointSource(n).Location.Srcid = Me.m_GroupSource(i).SourceID And Sources.PointSources.PointSource(n).GetRate(name) <> 0 And Sources.PointSources.PointSource(n).Seleted = True Then
                    If Me.m_GroupSource(i).Selected = True Then
                        If Me.m_GroupSource(i).SourceType = 1 Then '线源
                            'Me.m_GroupSource(i).Line.CreateVolume()
                            'For j As Integer = 0 To Me.m_GroupSource(i).Line.ArrayVolumeSource.Length - 1
                            '    strG = strG & "SO SRCGROUP  " & Me.m_GroupID & "  " & Me.m_GroupSource(i).Line.ArrayVolumeSource(j).Location.Srcid & vbCrLf
                            '    m_IsZero = False
                            'Next
                            'Me.m_GroupSource(i).Line.ArrayVolumeSource = Nothing
                        Else
                            strG = strG & "SO SRCGROUP  " & Me.m_GroupID & "  " & Me.m_GroupSource(i).SourceID & vbCrLf
                            m_IsZero = False
                        End If
                    End If
                End If
            Next
        Next
        For i As Integer = 0 To Me.m_GroupSource.Length - 1
            '先找出对应的污染源来，通过污染源是否为0来进行判断是否作为源组
            For n As Integer = 0 To Sources.VolumeSources.VolumeSource.Length - 1
                If Sources.VolumeSources.VolumeSource(n).Location.Srcid = Me.m_GroupSource(i).SourceID And Sources.VolumeSources.VolumeSource(n).GetRate(name) <> 0 And Sources.VolumeSources.VolumeSource(n).Seleted = True Then
                    If Me.m_GroupSource(i).Selected = True Then
                        If Me.m_GroupSource(i).SourceType = 1 Then '线源
                            'Me.m_GroupSource(i).Line.CreateVolume()
                            'For j As Integer = 0 To Me.m_GroupSource(i).Line.ArrayVolumeSource.Length - 1
                            '    strG = strG & "SO SRCGROUP  " & Me.m_GroupID & "  " & Me.m_GroupSource(i).Line.ArrayVolumeSource(j).Location.Srcid & vbCrLf
                            '    m_IsZero = False
                            'Next
                            'Me.m_GroupSource(i).Line.ArrayVolumeSource = Nothing
                        Else
                            strG = strG & "SO SRCGROUP  " & Me.m_GroupID & "  " & Me.m_GroupSource(i).SourceID & vbCrLf
                            m_IsZero = False
                        End If
                    End If
                End If
            Next
        Next
        For i As Integer = 0 To Me.m_GroupSource.Length - 1
            '先找出对应的污染源来，通过污染源是否为0来进行判断是否作为源组
            For n As Integer = 0 To Sources.AreaSources.AreaSource.Length - 1
                If Sources.AreaSources.AreaSource(n).Location.Srcid = Me.m_GroupSource(i).SourceID And Sources.AreaSources.AreaSource(n).GetRate(name) <> 0 And Sources.AreaSources.AreaSource(n).Seleted = True Then
                    If Me.m_GroupSource(i).Selected = True Then
                        If Me.m_GroupSource(i).SourceType = 1 Then '线源
                            'Me.m_GroupSource(i).Line.CreateVolume()
                            'For j As Integer = 0 To Me.m_GroupSource(i).Line.ArrayVolumeSource.Length - 1
                            '    strG = strG & "SO SRCGROUP  " & Me.m_GroupID & "  " & Me.m_GroupSource(i).Line.ArrayVolumeSource(j).Location.Srcid & vbCrLf
                            '    m_IsZero = False
                            'Next
                            'Me.m_GroupSource(i).Line.ArrayVolumeSource = Nothing
                        Else
                            strG = strG & "SO SRCGROUP  " & Me.m_GroupID & "  " & Me.m_GroupSource(i).SourceID & vbCrLf
                            m_IsZero = False
                        End If
                    End If
                End If
            Next
        Next
        For i As Integer = 0 To Me.m_GroupSource.Length - 1
            '先找出对应的污染源来，通过污染源是否为0来进行判断是否作为源组
            For n As Integer = 0 To Sources.AreacircSources.AreaCircSource.Length - 1
                If Sources.AreacircSources.AreaCircSource(n).Location.Srcid = Me.m_GroupSource(i).SourceID And Sources.AreacircSources.AreaCircSource(n).GetRate(name) <> 0 And Sources.AreacircSources.AreaCircSource(n).Seleted = True Then
                    If Me.m_GroupSource(i).Selected = True Then
                        If Me.m_GroupSource(i).SourceType = 1 Then '线源
                            'Me.m_GroupSource(i).Line.CreateVolume()
                            'For j As Integer = 0 To Me.m_GroupSource(i).Line.ArrayVolumeSource.Length - 1
                            '    strG = strG & "SO SRCGROUP  " & Me.m_GroupID & "  " & Me.m_GroupSource(i).Line.ArrayVolumeSource(j).Location.Srcid & vbCrLf
                            '    m_IsZero = False
                            'Next
                            'Me.m_GroupSource(i).Line.ArrayVolumeSource = Nothing

                        Else
                            strG = strG & "SO SRCGROUP  " & Me.m_GroupID & "  " & Me.m_GroupSource(i).SourceID & vbCrLf
                            m_IsZero = False
                        End If
                    End If
                End If
            Next
        Next
        For i As Integer = 0 To Me.m_GroupSource.Length - 1
            '先找出对应的污染源来，通过污染源是否为0来进行判断是否作为源组
            For n As Integer = 0 To Sources.AreaPolySources.AreaPolySource.Length - 1
                If Sources.AreaPolySources.AreaPolySource(n).Location.Srcid = Me.m_GroupSource(i).SourceID And Sources.AreaPolySources.AreaPolySource(n).GetRate(name) <> 0 And Sources.AreaPolySources.AreaPolySource(n).Seleted = True Then
                    If Me.m_GroupSource(i).Selected = True Then
                        If Me.m_GroupSource(i).SourceType = 1 Then '线源
                            'Me.m_GroupSource(i).Line.CreateVolume()
                            'For j As Integer = 0 To Me.m_GroupSource(i).Line.ArrayVolumeSource.Length - 1
                            '    strG = strG & "SO SRCGROUP  " & Me.m_GroupID & "  " & Me.m_GroupSource(i).Line.ArrayVolumeSource(j).Location.Srcid & vbCrLf
                            '    m_IsZero = False
                            'Next
                            'Me.m_GroupSource(i).Line.ArrayVolumeSource = Nothing
                        Else
                            strG = strG & "SO SRCGROUP  " & Me.m_GroupID & "  " & Me.m_GroupSource(i).SourceID & vbCrLf
                            m_IsZero = False
                        End If
                    End If
                End If
            Next
        Next
        For i As Integer = 0 To Me.m_GroupSource.Length - 1
            '先找出对应的污染源来，通过污染源是否为0来进行判断是否作为源组
            For n As Integer = 0 To Sources.OpenPitSources.OpenPitSource.Length - 1
                If Sources.OpenPitSources.OpenPitSource(n).Location.Srcid = Me.m_GroupSource(i).SourceID And Sources.OpenPitSources.OpenPitSource(n).GetRate(name) <> 0 And Sources.OpenPitSources.OpenPitSource(n).Seleted = True Then
                    If Me.m_GroupSource(i).Selected = True Then
                        If Me.m_GroupSource(i).SourceType = 1 Then '线源
                            'Me.m_GroupSource(i).Line.CreateVolume()
                            'For j As Integer = 0 To Me.m_GroupSource(i).Line.ArrayVolumeSource.Length - 1
                            '    strG = strG & "SO SRCGROUP  " & Me.m_GroupID & "  " & Me.m_GroupSource(i).Line.ArrayVolumeSource(j).Location.Srcid & vbCrLf
                            '    m_IsZero = False
                            'Next
                            'Me.m_GroupSource(i).Line.ArrayVolumeSource = Nothing
                        Else
                            strG = strG & "SO SRCGROUP  " & Me.m_GroupID & "  " & Me.m_GroupSource(i).SourceID & vbCrLf
                            m_IsZero = False
                        End If
                    End If
                End If
            Next
        Next
        For i As Integer = 0 To Me.m_GroupSource.Length - 1
            '先找出对应的污染源来，通过污染源是否为0来进行判断是否作为源组
            For n As Integer = 0 To Sources.LineSources.ArrayLineSources.Length - 1
                If Sources.LineSources.ArrayLineSources(n).Location.Srcid = Me.m_GroupSource(i).SourceID And Sources.LineSources.ArrayLineSources(n).GetRate(name) <> 0 And Sources.LineSources.ArrayLineSources(n).Seleted = True Then
                    If Me.m_GroupSource(i).Selected = True Then
                        If Me.m_GroupSource(i).SourceType = 1 Then '线源
                            Sources.LineSources.ArrayLineSources(n).CreateVolume()
                            For j As Integer = 0 To Sources.LineSources.ArrayLineSources(n).ArrayVolumeSource.Length - 1
                                strG = strG & "SO SRCGROUP  " & Me.m_GroupID & "  " & Sources.LineSources.ArrayLineSources(n).ArrayVolumeSource(j).Location.Srcid & vbCrLf
                                m_IsZero = False
                            Next
                            Sources.LineSources.ArrayLineSources(n).ArrayVolumeSource = Nothing
                        Else
                            'strG = strG & "SO SRCGROUP  " & Me.m_GroupID & "  " & Me.m_GroupSource(i).SourceID & vbCrLf
                            'm_IsZero = False
                        End If
                    End If
                End If
            Next
        Next


        Return strG
    End Function
    ''' <summary>
    ''' 获取污染源组中选中的污染源个数
    ''' </summary>
    ''' <param name="Soruces"></param>
    ''' <param name="strName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetAllSlelectedCount(ByVal Soruces As Source, ByVal strName As String)
        Dim Count As Integer = 0
        If Me.m_GroupID.ToString.ToUpper = "ALL" Then
            For i As Integer = 0 To Me.m_GroupSource.Length - 1
                If Soruces.GetRateFormID(m_GroupSource(i).SourceID, strName) <> 0 Then '由于线源最后要转换成体源，所以在这里获取的是线源转换成体源后的数据
                    If m_GroupSource(i).SourceType = 1 Then
                        Count = Count + CType(Soruces.GetSourceFormID(m_GroupSource(i).SourceID, strName), Line).GetVolumesCount()
                    Else
                        Count += 1
                    End If
                End If
            Next
        Else
            For i As Integer = 0 To Me.m_GroupSource.Length - 1
                If m_GroupSource(i).Selected = True And Soruces.GetRateFormID(m_GroupSource(i).SourceID, strName) <> 0 Then
                    If m_GroupSource(i).SourceType = 1 Then
                        Count = Count + CType(Soruces.GetSourceFormID(m_GroupSource(i).SourceID, strName), Line).GetVolumesCount()
                    Else
                        Count += 1
                    End If

                End If
            Next
        End If

        Return Count
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj As New Group
        obj.m_Name = Me.m_Name
        obj.m_GroupID = Me.m_GroupID
        ReDim obj.m_GroupSource(Me.m_GroupSource.Length - 1)
        For i As Integer = 0 To Me.m_GroupSource.Length - 1
            obj.m_GroupSource(i) = Me.m_GroupSource(i).Clone
        Next
        obj.m_IsTypeDay = Me.m_IsTypeDay
        obj.m_IsTypeHours = Me.m_IsTypeHours
        obj.m_IsDayOver = Me.m_IsDayOver
        obj.m_IsHoursOver = Me.m_IsHoursOver
        Return obj
    End Function
End Class


''' <summary>
''' 组中的污染源结构
''' </summary>
''' <remarks></remarks>
<Serializable()> Public Class GroupSource
    Implements ICloneable

    Public SourceName As String
    Public SourceID As String
    Public Selected As Boolean
    ''' <summary>
    ''' 污染源类型。0为常规污染源，1为线源。对线源要进行特殊的处理。
    ''' </summary>
    ''' <remarks></remarks>
    Public SourceType As Integer = 0 '

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj As New GroupSource
        obj.SourceName = Me.SourceName
        obj.SourceID = Me.SourceID
        obj.Selected = Me.Selected
        obj.SourceType = Me.SourceType
        Return obj
    End Function
End Class